package file;

import java.io.*;
import java.util.Scanner;

public class Demo12 {
    //文件复制
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入复制文件的源路径：");
        String sourcePath= sc.next();
        File sourceFile=new File(sourcePath);
        System.out.println("请输入目标路径：");
        String destPath= sc.next();
        File destFile=new File(destPath);
        if(sourceFile.isDirectory()||!sourceFile.isFile()){
            System.out.println("不是普通文件请重新输入");
            return;
        }
        if(destFile.exists()){
            if(destFile.isDirectory()){
                System.out.println("目标文件是一个目录，请确认路径是否正确");
                return;
            }
        }
        try(InputStream inputStream=new FileInputStream(sourceFile);OutputStream outputStream=new FileOutputStream(destFile)) {
            while(true){
                byte[] buf=new byte[1024];
                int n=inputStream.read(buf);
                if(n==-1){
                    break;
                }
                outputStream.write(buf,0,n);
            }
            outputStream.flush();
        }  catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("文件复制完成!");
    }
}
